﻿#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class EditorCommonFunctions {

    public static void SavePrefab<T>(string dataFilePath, System.Action<T> updateData) where T : MonoBehaviour
    {
        UnityEngine.GameObject obj = null;
        UnityEngine.GameObject dataPrefab = AssetDatabase.LoadAssetAtPath<UnityEngine.GameObject>(dataFilePath);
        if (dataPrefab != null)
        {
            obj = UnityEngine.GameObject.Instantiate(dataPrefab) as UnityEngine.GameObject;
        }
        else
        {
            dataPrefab = PrefabUtility.CreateEmptyPrefab(dataFilePath) as UnityEngine.GameObject;
            string fileName = System.IO.Path.GetFileNameWithoutExtension(dataFilePath);
            Debug.Log("fileName :" + fileName);
            obj = new UnityEngine.GameObject(fileName);
            obj.AddComponent<T>();
        }
        updateData(obj.GetComponent<T>());

        PrefabUtility.ReplacePrefab(obj, dataPrefab);
        UnityEngine.GameObject.DestroyImmediate(obj);

        AssetDatabase.Refresh();
    }
}
#endif